package net.anzix.jimposition.transformations;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import javax.swing.JPanel;
import com.lowagie.text.pdf.PdfTemplate;
import net.anzix.jimposition.BasicTransformation;

public class ParityFilter extends AbstractFilter {

    public ParityFilter() {
        setAttribute("padding", "0");
    }

    public String toString() {
        return "Select pages by parity";
    }

    public void setPanelSettings() {
        ((ParityFilterPanel) getPanel()).getOddPagesRadio().setSelected((Boolean) getAttribute("odd"));
        ((ParityFilterPanel) getPanel()).getEvenPagesRadio().setSelected((Boolean) getAttribute("even"));
    }

    public void getPanelSettings() {
        setAttribute("odd", Boolean.valueOf(((ParityFilterPanel) getPanel()).getOddPagesRadio().isSelected()));
        setAttribute("even", Boolean.valueOf(((ParityFilterPanel) getPanel()).getEvenPagesRadio().isSelected()));

    }

    public void process(PdfContentByte cb, PdfWriter writer, PdfReader inputReader, int page) {
        cb.addTemplate(writer.getImportedPage(inputReader, page), 1.0f, 0f, 0f, 1.0f, 0f, 0f);
        cb.stroke();
    }

    public JPanel createPanel() {
        return new ParityFilterPanel();
    }

    public int getOldPageNo(int page) {
        if ((Boolean) getAttribute("even") && page % 2 == 0) {
            return page;
        } else if (!(Boolean) getAttribute("odd") && page % 2 == 1) {
            return page;
        } else {
            return -1;
        }
    }
}
